package com.lzh.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzh.commonutils.CourseInfo;
import com.lzh.edu.entity.Course;
import com.lzh.edu.entity.CourseDescription;
import com.lzh.edu.entity.Video;
import com.lzh.edu.mapper.CourseMapper;
import com.lzh.edu.query.CourseQuery;
import com.lzh.edu.service.ChapterService;
import com.lzh.edu.service.CourseDescriptionService;
import com.lzh.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.edu.service.VideoService;
import com.lzh.edu.vo.CourseInfoVo;
import com.lzh.edu.vo.course.CourseFrontVo;
import com.lzh.edu.vo.course.CoursePublishVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lizuhu
 * @since 2021-04-24
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseDescriptionService courseDescriptionService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private ChapterService chapterService;

    /**
     * 添加课程
     * @param courseInfoVo 课程对象
     * @return 返回课程id
     */
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加课程信息
        //将courseInfoVo转换为course
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        int insert = baseMapper.insert(course);
        if (insert == 0) {
            try {
                throw new Exception("添加课程失败");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String cid = course.getId();
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);
        return cid;
    }

    /**
     * 课程回显
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //查询课程表
        Course course = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //将课程封装到Vo对象
        if (course != null) BeanUtils.copyProperties(course,courseInfoVo);
        //查询描述表
        CourseDescription description = courseDescriptionService.getById(courseId);
        //将课程简介封装到Vo对象
        if (description != null) courseInfoVo.setDescription(description.getDescription());
        return courseInfoVo;
    }

    /**
     * 课程修改
     * @param courseInfoVo
     */
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        boolean b = this.updateById(course);
        //修改描述表
        CourseDescription description = new CourseDescription();
        description.setDescription(courseInfoVo.getDescription());
        description.setId(course.getId());
        boolean b1 = courseDescriptionService.updateById(description);
    }

    /**
     * 获取最终的需要发布的课程
     * @param courseId
     * @return
     */
    @Override
    public CoursePublishVo getPublishCourse(String courseId) {
       return baseMapper.getPublishCourseById(courseId);
    }

    /**
     * 发布课程
     * @param courseId
     */
    @Override
    public Boolean publishCourse(String courseId) {
        Course course = new Course();
        course.setId(courseId);
        course.setStatus(Course.COURSE_NORMAL);
        int count = baseMapper.updateById(course);
        return count > 0;

    }

    @Override
    public void pageQuery(Page<Course> pageParam, CourseQuery courseQuery) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("gmt_create");
        if (courseQuery == null) {
            baseMapper.selectPage(pageParam,queryWrapper);
            return;
        }
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(teacherId) ) {
            queryWrapper.eq("teacher_id", teacherId);
        }

        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.eq("subject_parent_id", subjectParentId);
        }

        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.eq("subject_id", subjectId);
        }

        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public Boolean removeCourseById(String id) {
        //删除所有云端视频 TODO
        videoService.removeVideoByCourseId(id);
        //删除所有章节
        chapterService.removeCourseById(id);
        //删除课程描述
        courseDescriptionService.removeById(id);
        Integer result = baseMapper.deleteById(id);
        return result != null && result > 0;
    }

    /**
     * 前台查询课程
     * @return
     */
    @Cacheable(value = "course",key = "'courseList'")
    @Override
    public List<Course> getcourseByLimit() {
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.orderByDesc("id");
        courseQueryWrapper.last("limit 8");
        List<Course> courseList = baseMapper.selectList(courseQueryWrapper);
        return courseList;
    }

    @Override
    public List<Course> selectListByTeacherId(String id) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id",id);
        //按照最后更新时间倒序排列
        wrapper.orderByDesc("gmt_modified");
        List<Course> courseList = baseMapper.selectList(wrapper);
        return courseList;
    }

    @Override
    public void pageList(Page<Course> coursePage) {
        baseMapper.selectPage(coursePage, null);
    }

    //查询课程信息
    @Override
    public CourseFrontVo selectInfoWebById(String id) {
        this.updatePageViewCount(id);
        return baseMapper.selectInfoWebById(id);
    }

    //更新课程浏览量
    @Override
    public void updatePageViewCount(String id) {
        Course course = baseMapper.selectById(id);
        course.setViewCount(course.getViewCount() + 1);
        baseMapper.updateById(course);
    }
    //查询课程信息生成订单
    @Override
    public CourseInfo getCourseByOrder(String id) {
        //查询课程表
        Course course = baseMapper.selectById(id);
        CourseInfo courseInfo = new CourseInfo();
        //将课程封装到Vo对象
        if (course != null) BeanUtils.copyProperties(course,courseInfo);
        return courseInfo;
    }
}
